import { locations } from "../../../gpu/locations";
const Geometry = /*wgsl*/ `
#include <VertexInput>
#include <VertexOutput>
#include <DisplacementMap_def>
#include <Camera>
#include <Animation>
#include <Trans_def>
fn isPerspectiveMatrix(m:mat4x4<f32> )->bool {
	return m[ 2 ][ 3 ] == - 1.0;
}
//精准光源的光线
#if ~{def.lightCastCount}
struct LightRay {
  color:vec3<f32>,//0
  t:f32,//type //3
  position:vec3<f32>,//4
  intensity:f32,//7
  at:vec3<f32>,//8
  range:f32,//11
  e1:f32,//12   //面光源中e1,e2对应width,height  聚光灯中对应innerCos outerCos
  e2:f32,//13
  e3:f32,//14
  e4:f32,//15
  lightViewProjMatrix:mat4x4<f32>
}
#else
struct LightRay {
  color:vec3<f32>,//0
  t:f32,//type //3
  position:vec3<f32>,//4
  intensity:f32,//7
  at:vec3<f32>,//8
  range:f32,//11
  e1:f32,//12   //面光源中e1,e2对应width,height  聚光灯中对应innerCos outerCos
  e2:f32,//13
  e3:f32,//14
  e4:f32//15
}
#endif
// 灯光部分
#if ~{def.punctualLight}

//精准光源
struct PunctusLight{
  lights:array<LightRay, 10>,
}
@group(${locations.punctualLight[0]}) @binding(${locations.punctualLight[1]}) var<uniform> punctusLight:PunctusLight;
#endif
var <private> skinMatrix:mat4x4<f32>;
 fn getPosition(input:VertexInput)->vec4<f32>{
   var pos:vec4<f32> = vec4(input.position, 1.0);
  //  如果有变形动画
   #if ~{def.u_morphWeights}
    pos += morphPosition(input.VertexID);
   #endif
     //  如果有骨骼动画
   #if ~{def.jointMatrixs}
    skinMatrix=getSkinningMatrix(input);
    pos = skinMatrix* pos;
   #endif
   #if ~{def.displacementMap && def.normal && def.texCood0}
    pos = vec4(pos.xyz+cal_displacementTrans(input),1.0);
   #endif 
    return pos;
  }
  #if ~{def.normal}
  fn getNormal(input:VertexInput)->vec3<f32>{
    var normal:vec3<f32> = input.normal;
    //  如果有变形动画
    #if ~{def.u_morphWeights}
     normal += morphNormal(input.VertexID);
    #endif
    //  如果有骨骼动画
    #if ~{def.jointMatrixs}
    // var tempNormal=inverse_transpose_4x4(skinMatrix)*vec4<f32>(normal,1.0);
    // normal = tempNormal.xyz;
    #endif
    return normalize(normal);
  }
  #endif
  #if ~{def.tangent&&def.normal}
  fn getTangent(input:VertexInput)->vec3<f32>{
    var  tangent:vec3<f32> = input.tangent.xyz;
    //  如果有变形动画
    #if ~{def.u_morphWeights}
      tangent += morphTangent(input.VertexID);
    #endif
    //  如果有骨骼动画
    #if ~{def.jointMatrixs}
      var tempTangent=skinMatrix*vec4<f32>(tangent,1.0);
      tangent = tempTangent.xyz;
    #endif
      return normalize(tangent);
  }
  #endif
@vertex
    fn main(vertexInput:VertexInput) -> VertexOutput {
      var output:VertexOutput;
      var modelMatrix:mat4x4<f32>;
      var u_NormalMatrix:mat4x4<f32>;
      // 从trans中分解处模型矩阵与法向量矩阵
      #if ~{def.trans}
      modelMatrix=trans.modelMat;
      u_NormalMatrix=trans.normalMat;
      #endif
      #if ~{def.transinstance}
      modelMatrix=trans[vertexInput.index].modelMat;
      u_NormalMatrix=trans[vertexInput.index].normalMat;
      #endif
      output.orgin=vec3<f32>(modelMatrix[3].xyz);
      var position= getPosition(vertexInput);
//       var direction = camera.position- output.orgin;
//       // 计算旋转角度  
//       var angle = atan2(direction.y, direction.x);
//        // 计算旋转矩阵  
//       let c = cos(angle);
//       let s = sin(angle);
//    // // 创建旋转矩阵  
//    var rotationMatrix = mat2x2<f32>(c, -s, s, c); // 只涉及 x 和 y，z 轴的值保持不变  
//       // 应用旋转  
// // 应用旋转  
// var rotatedPosition = rotationMatrix * vec2(position.x, position.y);  
//     // 更新位置  
// position = vec4(rotatedPosition.x, rotatedPosition.y, position.z, 1.0); // z 轴保持不变  
      var pos= modelMatrix*position;
      output.Position =camera.vp*pos;
      output.fragPosition=pos.xyz/pos.w;
      // 对数深度缓冲
      #if ~{def.logarithmicDepth}
        output.flogz = 1.0 + output.Position.w;
      #endif
      #if ~{def.normal}
      var  normal= getNormal(vertexInput);
      #else
      var  normal:vec3<f32>= vec3(0,0,1);
      #endif
        // 如果定义法向量与切线
      #if ~{def.normal} 
        output.normal=normalize((u_NormalMatrix* vec4(normal, 1.0)).xyz);
      #endif 
      #if ~{def.tangent} 
        var  tangent= getTangent(vertexInput);
        output.tangent=normalize((modelMatrix * vec4(tangent, 1.0) ).xyz);
      #endif 
      #if ~{def.texCood0}
      output.texCood0=vertexInput.texCood0;
      #endif 
      #if ~{def.texCood1}
      output.texCood1=vertexInput.texCood1;
      #endif 
      //  如果有变形动画
      #if ~{def.u_morphWeights&&def.texCood0}
      output.texCood0 += morphTexCoord0(vertexInput.VertexID);
      // output.texCood1 += morphTexCoord1(vertexInput.VertexID);
      #endif 
      #if ~{def.color}
        #if ~{def.colorF32}
         #if ~{def.colorVec3}
          output.color=vec4<f32>(vertexInput.color.xyz,1.0)  ;
         #else 
          output.color=vec4<f32>(vertexInput.color);
         #endif 
        #else 
          #if ~{def.colorVec3}
            output.color=vec4<f32>(vertexInput.color.x/255,vertexInput.color.y/255,vertexInput.color.z/255,1.0)  ;
          #else 
            output.color=vec4<f32>(vertexInput.color/255);
          #endif 
        #endif 
        // #if ~{def.u_morphWeights}
        // output.color = clamp(output.color + morphColor0(vertexInput.VertexID).xyz, 0.0, 1.0);
        // #endif
      #endif 
      return output;
    }
`;
export { Geometry };
